//package net.zigin.epi.demo.config;
//
//import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
//import net.zigin.epi.demo.constants.RedisKeyConstants;
//import net.zigin.epi.demo.service.RedisReceiverService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
//import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
//import org.springframework.boot.context.properties.EnableConfigurationProperties;
//import org.springframework.cache.CacheManager;
//import org.springframework.cache.annotation.CachingConfigurerSupport;
//import org.springframework.cache.annotation.EnableCaching;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.data.redis.cache.RedisCacheConfiguration;
//import org.springframework.data.redis.cache.RedisCacheManager;
//import org.springframework.data.redis.connection.RedisConnectionFactory;
//import org.springframework.data.redis.core.*;
//import org.springframework.data.redis.listener.ChannelTopic;
//import org.springframework.data.redis.listener.RedisMessageListenerContainer;
//import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
//import org.springframework.data.redis.serializer.RedisSerializationContext;
//import org.springframework.data.redis.serializer.RedisSerializer;
//import org.springframework.data.redis.serializer.StringRedisSerializer;
//
//import java.time.Duration;
//
///**
// * redis 配置类
// *
// * @author mgw1986
// * @version V1.0
// * @date 2018/12/3 21:55
// */
//@Configuration
//@EnableCaching
//@ConditionalOnClass(RedisOperations.class)
//@EnableConfigurationProperties(RedisProperties.class)
//public class RedisConfiguration extends CachingConfigurerSupport {
//    @Autowired
//    private RedisConnectionFactory redisConnectionFactory;
//
//    @Bean
//    public CacheManager cacheManager() {
//        // 无过期时间
//        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
//                .entryTtl(Duration.ofMinutes(30))
//                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(keySerializer()))
//                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(valueSerializer()));
//        RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager
//                .builder(redisConnectionFactory)
//                .cacheDefaults(config);
//        return builder.build();
//    }
//
//
//    @Bean
//    public <T> RedisTemplate<String, T> redisTemplate() {
//        RedisTemplate<String, T> template = new RedisTemplate<>();
//        template.setKeySerializer(keySerializer());
//        template.setHashKeySerializer(keySerializer());
//        template.setDefaultSerializer(valueSerializer());
//        template.setConnectionFactory(redisConnectionFactory);
//        template.afterPropertiesSet();
//        return template;
//    }
//
//    @Bean
//    @ConditionalOnBean(RedisReceiverService.class)
//    public ChannelTopic channelTopic() {
//        return new ChannelTopic(RedisKeyConstants.QUEUE_CHANNEL_TOPIC);
//    }
//
//    @Bean
//    @ConditionalOnBean(RedisReceiverService.class)
//    public MessageListenerAdapter messageListenerAdapter(RedisReceiverService redisReceiverService) {
//        return new MessageListenerAdapter(redisReceiverService, RedisKeyConstants.QUEUE_CHANNEL_RECEIVE_METHOD);
//    }
//
//    @Bean
//    @ConditionalOnBean(RedisReceiverService.class)
//    public RedisMessageListenerContainer redisMessageListenerContainer(MessageListenerAdapter messageListenerAdapter) {
//        RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
//        redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);
//        redisMessageListenerContainer.addMessageListener(messageListenerAdapter, channelTopic());
//        return redisMessageListenerContainer;
//    }
//
//    @Bean
//    public <T> HashOperations<String, String, T> hashOperations(RedisTemplate<String, T> redisTemplate) {
//        return redisTemplate.opsForHash();
//    }
//
//    @Bean
//    public <T> ValueOperations<String, T> valueOperations(RedisTemplate<String, T> redisTemplate) {
//        return redisTemplate.opsForValue();
//    }
//
//    @Bean
//    public <T> ListOperations<String, T> listOperations(RedisTemplate<String, T> redisTemplate) {
//        return redisTemplate.opsForList();
//    }
//
//    @Bean
//    public <T> SetOperations<String, T> setOperations(RedisTemplate<String, T> redisTemplate) {
//        return redisTemplate.opsForSet();
//    }
//
//    @Bean
//    public <T> ZSetOperations<String, T> zSetOperations(RedisTemplate<String, T> redisTemplate) {
//        return redisTemplate.opsForZSet();
//    }
//
//
//    private RedisSerializer<String> keySerializer() {
//        return new StringRedisSerializer();
//    }
//
//    private RedisSerializer valueSerializer() {
//        return new GenericFastJsonRedisSerializer();
//    }
//}
